#pragma once

static BOOL WriteBMP(const char* pszBmpFileName, unsigned char* pOutImage, int nWidth, int nHeight, int nBitCount = 24)
{
	unsigned char* pBuffer = pOutImage;
	FILE* fp = fopen(pszBmpFileName, "wb");
	if(fp == NULL)
		return FALSE;

	unsigned long lImageSize = nWidth * nHeight * nBitCount / 8;

	BITMAPFILEHEADER bfh = {0};
	bfh.bfType = 'MB';
	bfh.bfSize = sizeof(bfh) + lImageSize + sizeof(BITMAPINFOHEADER);
	bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
	fwrite(&bfh, 1, sizeof(bfh), fp);

	BITMAPINFOHEADER bih = {0};
	bih.biSize = sizeof(bih);
	bih.biWidth = nWidth;
	bih.biHeight = nHeight;
	bih.biPlanes = 1;
	bih.biBitCount = nBitCount;
	fwrite(&bih, 1, sizeof(bih), fp);

	/* write first plane */
	fwrite(pBuffer, 1, lImageSize, fp);
	fclose(fp);

	return TRUE;
}


